package com.example.springboot3.common;

/**
 * 统一的返回数据类型
 */
public class Result {
    //状态码
    private String code;
    //报错信息
    private String msg;
    //返回数据
    private Object data;


    public Result(){

    }

    //请求成功
    public static Result success(){
        Result result = new Result();
        result.setCode("200");
        result.setMsg("请求成功");
        return result;
    }

    //请求成功返回-数据
    public static Result success(Object data){
        Result result = success();
        result.setData(data);
        return result;
    }

    //请求失败
    public static Result error(){
        Result result = new Result();
        result.setCode("500");
        result.setMsg("请求失败");
        return result;
    }

    //请求失败返回-数据
    public static Result error(String code ,String msg){
        Result result = error();
        result.setMsg(msg);
        return result;
    }

    //默认500返回错误信息
    public static Result error(String msg){
        Result result = error();
        result.setCode("500");
        result.setMsg(msg);
        return result;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
